#include "student_submit_dialog.h"
#include <QMimeData>
#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QDragEnterEvent>
#include <QDropEvent>

StudentSubmitDialog::StudentSubmitDialog(const QString &studentAccount, QWidget *parent)
    : QDialog(parent), m_account(studentAccount)
{
    ui.setupUi(this);
    setAcceptDrops(true);
    connect(ui.btnBack, &QPushButton::clicked, this, &QDialog::accept);
}

void StudentSubmitDialog::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
}

void StudentSubmitDialog::dropEvent(QDropEvent *event)
{
    QList<QUrl> urls = event->mimeData()->urls();
    if (urls.isEmpty()) return;
    QString filePath = urls.first().toLocalFile();
    if (filePath.isEmpty()) return;
    QFileInfo fileInfo(filePath);
    QString ext = fileInfo.suffix();
    QString destPath = QDir::currentPath() + "/" + m_account + (ext.isEmpty() ? "" : "." + ext);
    if (QFile::copy(filePath, destPath)) {
        QMessageBox::information(this, "成功", "文件已上传为: " + QFileInfo(destPath).fileName());
    } else {
        QMessageBox::warning(this, "失败", "文件上传失败，可能已存在同名文件。");
    }
} 